﻿/**
@author: dxqcmd
解析流程图
*/


/*
该js用来解析流程图为 数组 结构如下:
    arrs[0] = "actId";
    arrs[1] = "actName";
    arrs[2] = "mainType";
    arrs[3] = "type";
    arrs[4] = "out";
    arrs[5] = "in";
    arrs[6] = "express";
    arrs[7] = "users";
    arrs[8] = "nextAct";
    arrs[9] = "module";
*/

/*
<wfact id="act1" name="某某活动" type="route" wfin="N" wfout="Y">
		<wfdatabody >
				<wfModule><!--活动要绑定或者对应的模块-->
					<wfmodid>1</wfmodid>
					<wfurl>/localhost:8080/kk.html</wfurl>
					<wfdesc>kk页面</wfdesc>
				</wfModule>
        
				<wfusers><!--用户列表-->
					<wfuser id="wfu_1">
						<wfexpress></wfexpress>
						<wfdesc>一级办理人员 同时是 五华区人们政府的</wfdesc>
					</wfuser>
				</wfusers>		

				<wfnextact nextid="nextActId">conditions</wfnextact>
				<wfnextact nextid="nextActId2">conditions2</wfnextact>
		</wfdatabody>
		<wfpreact preid="preID1"></wfpreact>
    </wfact>
*/
function convertToList() {
    var acts = $id("xmlarea").querySelectorAll(tagwfact);
    if (!acts || acts.length < 1) return;

    var count = acts.length;

    var startNum = 0;
    var endNum = 0;
    for (var i = 0; i < count; i++) {
        var type = acts[i].getAttribute(attrType);
        //先判断是 开始活动==1 结束活动>=1
        if (type== typeActStart) {
            startNum++;
            if (startNum > 1) {
                alert("开始活动只允许有一个 !");
                return;
            }
            var preActs = acts[i].querySelectorAll(tagwfpreact);
            if (preActs && preActs.length>0) {
                alert("开始活动不允许有前置活动 !");
                return;
            }
        } else if (type == typeActEnd) {
            endNum++;
            var nextact = acts[i].querySelectorAll(tagwfnextact);
            var preacts = acts[i].querySelectorAll(tagwfpreact);
            if (nextact && nextact.length > 0) {
                alert("结束活动不允许有后置活动 !");
                return;
            } else if (!preacts || preacts < 1) {
                alert("结束活动没有前置活动 !");
                return;
            }
        } else if (type == typeActRollback) {
            var nextact = acts[i].querySelectorAll(tagwfnextact);
            var preacts = acts[i].querySelectorAll(tagwfpreact);
            if (nextact && nextact.length > 0) {
                alert("回退活动不允许有后置活动 !");
                return;
            } else if (!preacts || preacts < 1) {
                alert("回退活动没有前置活动 !");
                return;
            }
        } else {
            /*
        检查是否存在孤立点或断开部分
        ==>  除"开始活动外",任何活动都有"前置活动"(包括结束活动,回退活动都有前置活动)
        */
            if (!acts[i].querySelectorAll(tagwfpreact)) {
                alert("活动"+acts[i].getAttribute(attrName)+"没有前置活动 !");
                return;
            }
        }
        
    }//循环活动
    if (endNum < 1) {
        alert("结束活动至少需要一个 !");
        return;
    }
    //---------------把xmlarea转为array吧
    var arrs = new Array();
    arrs[0] = "actId";
    arrs[1] = "actName";
    arrs[2] = "mainType";
    arrs[3] = "type";
    arrs[4] = "out";
    arrs[5] = "in";
    arrs[6] = "users";  
    arrs[7] = "nextAct";
    arrs[8] = "module";
    //--------间隔为 10-------
    var span = 9;
    for (var i = 0; i < count; i++) {
        arrs[arrs.length] = acts[i].id;//活动id
        arrs[arrs.length] = acts[i].getAttribute(attrName);//活动名称
        arrs[arrs.length] = "";//活动主类型
        arrs[arrs.length] = acts[i].getAttribute(attrType);//活动子类型
        arrs[arrs.length] = acts[i].getAttribute(attrOut);//是否需要输出
        arrs[arrs.length] = acts[i].getAttribute(attrIn);//是否需要输入
        //-------用户---------------
        var users = acts[i].querySelectorAll(tagwfuser);
        if (users && users.length > 0) {
            var ulist = "";
            for (var n = 0; n < users.length; n++) {
                ulist += users[n].querySelector(tagwfexpress).innerHTML + ";";
            }
            arrs[arrs.length] = ulist.substr(0, ulist.length - 1);//权限用户表达式列表
        }
        else {
            arrs[arrs.length] = "";
        }
        //路径(下一活动)
        var paths = acts[i].querySelectorAll(tagwfnextact);
        if (paths && paths.length > 0) {
            var elist = "";
            for (var n = 0; n < paths.length; n++) {
                elist += paths[n].getAttribute(attrNextActId) + "{" + paths[n].innerHTML + "};";
            }
            arrs[arrs.length] = elist.substr(0, elist.length - 1);
        } else {
            arrs[arrs.length] = "";
        }
        //对应模块
        arrs[arrs.length] = acts[i].querySelector(tagwfmodid).innerHTML;//对应模块id
    }//outer for loop

    var htmObj = getHTMLObjects();
    if (htmObj) ns.SaveWf(arrs, htmObj, $id("xmlholder").innerHTML);
    else {
        return;
    }
}

function getHTMLObjects() {
    var blocks = $id("mainCanvas_innerDiv").innerHTML;
    var htmls = "";
    //----------------------------------------------------------
    var acts = $more(tagwfact);
    var count = acts ? acts.length : 0;
    if (count == 0) return "";
    for (var i = 0; i < count; i++) {
        var _paths = acts[i].querySelectorAll(tagwfnextact);
        if(_paths && _paths.length<1) continue;
        for (var n = 0; n < _paths.length; n++) {
            var startId = acts[i].id;
            var endId = _paths[n].getAttribute(attrNextActId);
            htmls += "<div id='"+prefixWfPath+startId+endId+"' class='connector " +"div"+ startId+ " div" + endId + "'>";
            var img = $single("img[" + attrWfpath + "=" + prefixWfPath + startId + endId);
            var lab = $id(prefixwfLabId + startId + endId);
            if (img) {
                var style = img.getAttribute("style");
                img.src = replaceString(img.src, "_r", "");
                img.removeAttribute("style");
                htmls += img.outerHTML;
                img.setAttribute("style", style);
            }
            if (lab) {
                var style = lab.getAttribute("style");
                lab.removeAttribute("style");
                htmls += lab.outerHTML;
                lab.setAttribute("style", style);
            }
            htmls += "</div>";
        }//end outer loop
    }

  
    blocks += htmls;
    return blocks;
}

